package com.cn.core.lambda;

import com.cn.core.utils.ClassUtils;
import com.cn.core.utils.SerializationUtils;
import lombok.Getter;

import java.io.*;

public class SerializedLambda implements Serializable {
    private static final long serialVersionUID = 8025925345765570181L;
    /**
     * 捕获类--等于实现的类
     */
    private Class<?> capturingClass;
    /**
     * 函数接口类
     */
    private String functionalInterfaceClass;
    /**
     * 函数接口方法名称
     */
    private String functionalInterfaceMethodName;
    /**
     * 函数接口方法签名
     */
    private String functionalInterfaceMethodSignature;
    /**
     * 实现的类名称
     */
    private String implClass;
    /**
     * 实现的方法
     */
    @Getter
    private String implMethodName;

    /**
     * 实现类的签名
     */
    private String implMethodSignature;
    /**
     * impl 方法种类
     */
    private int implMethodKind;
    /**
     * 实例化方法类型
     */
    private String instantiatedMethodType;

    private Object[] capturedArgs;

    public SerializedLambda() {
    }



    public String getFunctionalInterfaceClassName() {
        return this.normalizedName(this.functionalInterfaceClass);
    }

    /**
     * 获取 Impl 类
     *
     * @return {@link Class }<{@link ? }>
     */
    public Class<?> getImplClass() {
        return ClassUtils.toClassConfident(this.getImplClassName());
    }

    public String getImplClassName() {
        return this.normalizedName(this.implClass);
    }

    /**
     * 规范化名称
     *
     * @param name 名字
     * @return {@link String }
     */
    private String normalizedName(String name) {
        return name.replace('/', '.');
    }

    /**
     * 获取实例化类型
     *
     * @return {@link Class }<{@link ? }>
     */
    public Class<?> getInstantiatedType() {
        String instantiatedTypeName = this.normalizedName(this.instantiatedMethodType.substring(2, this.instantiatedMethodType.indexOf(59)));
        return ClassUtils.toClassConfident(instantiatedTypeName);
    }

    public String toString() {
        String interfaceName = this.getFunctionalInterfaceClassName();
        String implName = this.getImplClassName();
        return String.format("%s -> %s::%s", interfaceName.substring(interfaceName.lastIndexOf(46) + 1), implName.substring(implName.lastIndexOf(46) + 1), this.implMethodName);
    }
}